<!--
 * @Description: 
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-24 13:43:38
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-07 17:29:35
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增企业信息' : '编辑企业信息'" width="700px">
    <el-form ref="form" :model="form" size="small" label-width="100px">
      <el-form-item label="企业名称">
        <el-input v-model="form.corp_name" style="width:85%" />
      </el-form-item>
      <el-form-item label="企业corpid">
        <el-input v-model="form.corpid" style="width:85%" />
      </el-form-item>
      <el-form-item label="默认agent">
        <el-input v-model="form.agentid" style="width:85%" />
      </el-form-item>
      <el-form-item label="默认secret">
        <el-input v-model="form.secret" style="width:85%" />
      </el-form-item>
      <el-form-item label="通讯录secret">
        <el-input v-model="form.txl_secret" style="width:85%" />
      </el-form-item>
      <el-form-item label="是否启用">
        <el-input v-model="form.enabled" style="width:85%" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import jsonEditor from '@/views/components/jsonEditor'

export default {
  components: { jsonEditor },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        username: '',
        password: '',
        corp_name: '',
        corpid: '',
        agentid: '',
        secret: '',
        txl_secret: '',
        enabled: ''
      },
      jsonEditor: false
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      if (typeof this.form.value === 'object') {
        this.form.value = JSON.stringify(this.form.value)
      }
      this.$api.enterprise.enterprise.add({
        data: this.form
      }).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      this.$api.enterprise.enterprise.edit({
        data: this.form
      }).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        username: '',
        password: '',
        corp_name: '',
        corpid: '',
        agentid: '',
        secret: '',
        txl_secret: '',
        enabled: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
